package com.zzqstart.cateservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzqstart.cateservice.entity.CateGrade;
import com.zzqstart.cateservice.service.CateGradeService;
import com.zzqstart.commonutils.JwtUtils;
import com.zzqstart.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-03-13
 */
@RestController
@RequestMapping("/cateservice/categrade")
public class CateGradeController {
    @Autowired
    private CateGradeService cateGradeService;

    @GetMapping("checkwhetherGrade/{recipeId}")
    public R checkwhetherGrade(@PathVariable String recipeId, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        QueryWrapper<CateGrade> wrapper = new QueryWrapper<>();
        wrapper.eq("recipe_id", recipeId);
        wrapper.eq("member_id", memberId);
        wrapper.select("grade");
        CateGrade cateGrade = cateGradeService.getOne(wrapper);
        if (cateGrade==null){
            return R.ok().data("isGrade",false);
        }
        BigDecimal grade = cateGrade.getGrade();
        System.out.println(grade.equals(0.00));
        if(!grade.equals(0.00)){
            return R.ok().data("grade", grade).data("isGrade",true);
        }else {
            return R.ok().data("isGrade",false);
        }
    }
}

